<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
class FriendsController extends AdminController {
	public function index(){
		$p     = I('p','1');
		$list = M('Friends')->order('sort')->page($p.',5')->select();
        $allcount = M('Friends')->count();
        $Page       = new \Think\Page($allcount,5);
        $show       = $Page->show();
        $this -> assign('list',$list);
        $this -> assign('show',$show);
		$this -> display();
	}
	public function add(){
		$title = I('title');
		$sort  = I('sort');
		if(empty($title)){
			$this -> error('标题不能为空');
		}
		if(empty($sort)){
			$this -> error('排序不能为空');
		}
		if(empty($_FILES['pic']['name'])) {
			$this -> error('图片不能为空');
		}
	    $pic = $this ->upload($_FILES['pic']);
	    $data['pic']      = $pic;
	    $data['sort']     = $sort;
	    $data['title']    = $title;
	    $data['pic']      = $pic;
	    $data['add_time'] = time();
	    if(!M('Friends')->add($data)){
	    	$this -> error('系统繁忙');
	    }
	    $this -> success('添加成功');
	}
	public function del(){
		$banner_id = I('banner_id');
		if(empty($banner_id)){
			$this -> error('系统繁忙');
		}
		if(!M('Banner')->where(array('banner_id'=>$banner_id))->delete()){
           $this -> error('系统繁忙');
		}
		$this -> success('删除成功');
	}
	public function info(){
		$banner_id = I('banner_id');
		$row = M('Banner')->where(array('banner_id'=>$banner_id))->find();
		$this -> ajaxReturn($row);
	}
	public function edit(){
		$banner_id = I('banner_id');
		$title = I('title');
		$sort  = I('sort');
		$status = I('status',1);
		$pic    = I('pic');
		if(empty($title)){
			$this -> error('标题不能为空');
		}
		if(empty($sort)){
			$this -> error('排序不能为空');
		}
		if(!empty($_FILES['file']['name'])) {
			$pic = $this ->upload($_FILES['file']);
		}
		if(empty($pic)){
			$this -> error('图片不能为空');
		}
	    
	    $data['pic']      = $pic;
	    $data['sort']     = $sort;
	    $data['title']    = $title;
	    $data['status']   = $status;
	    $data['pic']      = $pic;
	    $data['add_time'] = time();
	    if(!M('Banner')->where(array('banner_id'=>$banner_id))->save($data)){
	    	$this -> error('系统繁忙');
	    }
	    $this -> success('修改成功');
	}
	
}